home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr47 / masm.zip / ASM3.DOC < prev    next >
Text File  |  1993-04-07  |  19KB  |  340 lines

  1.  
  2.   PROGPREF SEGMENT   AT 0      ;Really a DSECT mapping the program prefix
  3.            ORG   PROGPREF+6
  4.   MEMSIZE  DW   ?              ;Size of available memory
  5.   PROGPREF ENDS
  6. Really, no matter whether the AT value represents truth or fiction, it is
  7. your responsibility, not the assembler's, to get set up a segment register
  8. so that you can really reach the storage in question.   So, you can't say
  9.          MOV  AL,EQUIP
  10. unless you first say something like
  11.          MOV  AX,BIOSAREA   ;BIOSAREA becomes a symbol with value 40H
  12.          MOV  ES,AX
  13.          ASSUME ES:BIOSAREA
  14. Enough about SEGMENT.  The END statement is simple.  It goes at the end of
  15. every assembly.  When you are assembling a subroutine, you just say
  16.          END
  17. but when you are assembling the main routine of a program you say
  18.         END label
  19. where 'label' is the place where execution is to begin.
  20. Another pseudo-op illustrated in the program is ASSUME.  ASSUME is like the
  21. USING statement in 370 assembler.  However, ASSUME can ONLY refer to seg-
  22. ment registers.  The assembler uses ASSUME information to decide whether to
  23. assemble segment override prefixes and to check that the data you are try-
  24. ing to access is really accessible.  In this case, we can reassure the
  25. assembler that both the CS and DS registers will address the section called
  26. HELLO at execution time.  Actually, the SS and ES registers will too, but
  27. the assembler never needs to make use of this information.
  28. I guess I have explained everything in the program except that ORG
  29. pseudo-op.  ORG means the same thing as it does in many assembly languages.
  30. It tells the assembler to move its location counter to some particular
  31. address.  In this case, we have asked the assembler to start assembling
  32. code hex 100 bytes from the start of the section called HELLO instead of at
  33. the very beginning.  This simply reflects the way COM programs are loaded.
  34. When a COM program is loaded by the system, the system sets up all four
  35. segment registers to address the same 64K of storage.  The first 100 hex
  36. bytes of that storage contains what is called the program prefix; this area
  37. is described in appendix E of the DOS manual.  Your COM program physically
  38. begins after this.  Execution begins with the first physical byte of your
  39. program; that is why the JMP instruction is there.
  40. Wait a minute, you say, why the JMP instruction at all?  Why not put the
  41. data at the end?  Well, in a simple program like this I probably could have
  42. gotten away with that.  However, I have the habit of putting data first and
  43. would encourage you to do the same because of the way the assembler has of
  44. assembling different instructions depending on the nature of the operand.
  45. IBM PC Assembly Language Tutorial                                        19
  46.  
  47.  
  48. Unfortunately, sometimes the different choices of instruction which can
  49. assemble from a single opcode have different lengths.  If the assembler has
  50. already seen the data when it gets to the instructions it has a good chance
  51. of reserving the right number of bytes on the first pass.  If the data is
  52. at the end, the assembler may not have enough information on the first pass
  53. to reserve the right number of bytes for the instruction.  Sometimes the
  54. assembler will complain about this, something like "Forward reference is
  55. illegal" but at other times, it will make some default assumption.  On the
  56. second pass, if the assumption turned out to be wrong, it will report what
  57. is called a "Phase error," a very nasty error to track down.  So get in the
  58. habit of putting data and equated symbols ahead of code.
  59. OK.  Maybe you understand the program now.  Let's walk through the steps
  60. involved in making it into a real COM file.
  61. 1.  The file should be created with the name HELLO.ASM (actually the name
  62.     is arbitrary but the extension .ASM is conventional and useful)
  63. 2.
  64.       ASM   HELLO,,;
  65.     (this is just one example of invoking the assembler; it uses the small
  66.     assembler ASM, it produces an object file and a listing file with the
  67.     same name as the source file.  I am not going exhaustively into how to
  68.     invoke the assembler, which the manual goes into pretty well.  I guess
  69.     this is the first time I mentioned that there are really two
  70.     assemblers; the small assembler ASM will run in a 64K machine and
  71.     doesn't support macros.  I used to use it all the time; now that I have
  72.     a bigger machine and a lot of macro libraries I use the full function
  73.     assembler MASM.  You get both when you buy the package).
  74. 3.  If you issue DIR at this point, you will discover that you have
  75.     acquired HELLO.OBJ (the object code resulting from the assembly) and
  76.     HELLO.LST (a listing file).  I guess I can digress for a second here
  77.     concerning the listing file.  It contains TAB characters.  I have found
  78.     there are two good ways to get it printed and one bad way.  The bad way
  79.     is to use LPT1: as the direct target of the listing file or to try
  80.     copying the LST file to LPT1 without first setting the tabs on the
  81.     printer.  The two good ways are to either
  82.     a.  direct it to the console and activate the printer with CTRL-PRTSC.
  83.         In this case, DOS will expand the tabs for you.
  84.     b.  direct to LPT1: but first send the right escape sequence to LPT1 to
  85.         set the tabs every eight columns.  I have found that on some early
  86.         serial numbers of the IBM PC printer, tabs don't work quite right,
  87.         which forces you to the first option.
  88. 4.
  89.           LINK  HELLO;
  90.     (again, there are lots of linker options but this is the simplest.  It
  91.     takes HELLO.OBJ and makes HELLO.EXE).  HELLO.EXE?  I thought we were
  92. IBM PC Assembly Language Tutorial                                        20
  93.  
  94.  
  95.     making a COM program, not an EXE program.  Right.  HELLO.EXE isn't
  96.     really executable; its just that the linker doesn't know about COM pro-
  97.     grams.  That requires another utility.  You don't have this utility if
  98.     you are using DOS 1.0; you have it if you are using DOS 1.1 or DOS 2.0.
  99.     Oh, by the way, the linker will warn you that you have no stack
  100.     segment.  Don't worry about it.
  101. 5.
  102.           EXE2BIN  HELLO HELLO.COM
  103.     This is the final step.  It produces the actual program you will exe-
  104.     cute.  Note that you have to spell out HELLO.COM; for a nominally
  105.     rational but actually perverse reason, EXE2BIN uses the default exten-
  106.     sion BIN instead of COM for its output file.  At this point, you might
  107.     want to erase HELLO.EXE; it looks a lot more useful than it is.
  108.     Chances are you won't need to recreate HELLO.COM unless you change the
  109.     source and then you are going to have to redo the whole thing.
  110. 6.
  111.           HELLO
  112.     You type hello, that invokes the program, it says
  113.           HELLO YOURSELF!!!
  114.     (oops, what did I do wrong....?)
  115.  
  116. What about subroutines?
  117. _______________________
  118. What about subroutines?
  119. What about subroutines?
  120. What about subroutines?
  121. I started with a simple COM program because I actually think they are easi-
  122. er to create than subroutines to be called from high level languages, but
  123. maybe its really the latter you are interested in.  Here, I think you
  124. should get comfortable with the assembler FIRST with little exercises like
  125. the one above and also another one which I will finish up with.
  126. Next you are ready to look at the interface information for your particular
  127. language.  You usually find this in some sort of an appendix.  For example,
  128. the BASIC manual has Appendix C on Machine Language Subroutines.  The
  129. PASCAL manual buries the information a little more deeply:  the interface
  130. to a separately compiled routine can be found in the Chapter on Procedures
  131. and Functions, in a subsection called Internal Calling Conventions.
  132. Each language is slightly different, but here are what I think are some
  133. common issues in subroutine construction.
  134. 1.  NEAR versus FAR?  Most of the time, your language will probably call
  135.     your assembler routine as a FAR routine.  In this case, you need to
  136.     make sure the assembler will generate the right kind of return.  You do
  137.     this with a PROC...ENDP statement pair.  The PROC statement is probably
  138.  
  139. IBM PC Assembly Language Tutorial                                        21
  140.  
  141.  
  142.     a good idea for a NEAR routine too even though it is not strictly
  143.     required:
  144.               FAR linkage:        |            NEAR linkage:
  145.                                   |
  146.     ARBITRARY SEGMENT             |  SPECIFIC  SEGMENT  PUBLIC
  147.               PUBLIC THENAME      |            PUBLIC THENAME
  148.               ASSUME CS:ARBITRARY |            ASSUME CS:SPECIFIC,DS:SPECIFIC
  149.     THENAME   PROC FAR            |            ASSUME ES:SPECIFIC,SS:SPECIFIC
  150.               ..... code and data |  THENAME   PROC NEAR
  151.     THENAME   ENDP                |            ..... code and data ....
  152.     ARBITRARY ENDS                |  THENAME   ENDP
  153.               END                 |  SPECIFIC  ENDS
  154.                                   |            END
  155.     With FAR linkage, it doesn't really matter what you call the segment.
  156.     you must declare the name by which you will be called in a PUBLIC pseu-
  157.     do-op and also show that it is a FAR procedure.  Only CS will be ini-
  158.     tialized to your segment when you are called.  Generally, the other
  159.     segment registers will continue to point to the caller's segments.
  160.     With NEAR linkage, you are executing in the same segment as the caller.
  161.     Therefore, you must give the segment a specific name as instructed by
  162.     the language manual.  However, you may be able to count on all segment
  163.     registers pointing to your own segment (sometimes the situation can be
  164.     more complicated but I cannot really go into all of the details).  You
  165.     should be aware that the code you write will not be the only thing in
  166.     the segment and will be physically relocated within the segment by the
  167.     linker.  However, all OFFSET references will be relocated and will be
  168.     correct at execution time.
  169. 2.  Parameters passed on the stack.  Usually, high level languages pass
  170.     parameters to subroutines by pushing words onto the stack prior to
  171.     calling you.  What may differ from language to language is the nature
  172.     of what is pushed (OFFSET only or OFFSET and SEGMENT) and the order in
  173.     which it is pushed (left to right, right to left within the CALL state-
  174.     ment).  However, you will need to study the examples to figure out how
  175.     to retrieve the parameters from the stack.  A useful fact to exploit is
  176.     the fact that a reference involving the BP register defaults to a ref-
  177.     erence to the stack segment.  So, the following strategy can work:
  178.       ARGS     STRUC
  179.                DW   3 DUP(?)  ;Saved BP and return address
  180.       ARG3     DW   ?
  181.       ARG2     DW   ?
  182.       ARG1     DW   ?
  183.       ARGS     ENDS
  184.            ...........
  185.                PUSH BP                 ;save BP register
  186.                MOV  BP,SP              ;Use BP to address stack
  187.                MOV   ...,[BP].ARG2     ;retrieve second argument
  188.                (etc.)
  189.  
  190. IBM PC Assembly Language Tutorial                                        22
  191.  
  192.  
  193.     This example uses something called a structure, which is only available
  194.     in the large assembler; furthermore, it uses it without allocating it,
  195.     which is not a well-documented option.  However, I find the above
  196.     approach generally pleasing.  The STRUC is like a DSECT in that it
  197.     establishes labels as being offset a certain distance from an arbitrary
  198.     point; these labels are then used in the body of code by beginning them
  199.     with a period; the construction ".ARG2" means, basically, " +
  200.     (ARG2-ARGS)."
  201.     What you are doing here is using BP to address the stack, accounting
  202.     for the word where you saved the caller's BP and also for the two words
  203.     which were pushed by the CALL instruction.
  204. 3.  How big is the stack?  BASIC only gives you an eight word stack to play
  205.     with.  On the other hand, it doesn't require you to save any registers
  206.     except the segment registers.  Other languages give you a liberal
  207.     stack, which makes things a lot easier.  If you have to create a new
  208.     stack segment for yourself, the easiest thing is to place the stack at
  209.     the end of your program and:
  210.          CLI                      ;suppress interrupts while changing the stack
  211.          MOV  SSAVE,SS            ;save old SS in local storage (old SP
  212.                                   ; already saved in BP)
  213.          MOV  SP,CS               ;switch
  214.          MOV  SS,SP               ;the
  215.          MOV  SP,OFFSET STACKTOP  ;stack
  216.          STI                      ;(maybe)
  217.     Later, you can reverse these steps before returning to the caller.  At
  218.     the end of your program, you place the stack itself:
  219.              DW   128 DUP(?)          ;stack of 128 words (liberal)
  220.     STACKTOP LABEL WORD
  221. 4.  Make sure you save and restore those registers required by the caller.
  222. 5.  Be sure to get the right kind of addressibility.  In the FAR call exam-
  223.     ple, only CS addresses your segment.  If you are careful with your
  224.     ASSUME statements the assembler will keep track of this fact and gener-
  225.     ate CS prefixes when you make data references; however, you might want
  226.     to do something like
  227.                     MOV AX,CS      ;get current segment address
  228.                     MOV DS,AX      ;To DS
  229.                     ASSUME DS:THISSEG
  230.     Be sure you keep your ASSUMEs in synch with reality.
  231.  
  232.  
  233.  
  234. IBM PC Assembly Language Tutorial                                        23
  235.  
  236.  
  237. Learning about BIOS and the hardware
  238. ____________________________________
  239. Learning about BIOS and the hardware
  240. Learning about BIOS and the hardware
  241. Learning about BIOS and the hardware
  242. You can't do everything with DOS calls.  You may need to learn something
  243. about the BIOS and about the hardware itself.  In this, the Technical Ref-
  244. erence is a very good thing to look at.
  245. The first thing you look at in the Technical Reference, unless you are
  246. really determined to master the whole ball of wax, is the BIOS listings
  247. presented in Appendix A. Glory be:  here is the whole 8K of ROM which deals
  248. with low level hardware support layed out with comments and everything.
  249. In fact, if you are just interested in learning what BIOS can do for you,
  250. you just need to read the header comments at the beginning of each section
  251. of the listing.
  252. BIOS services are invoked by means of the INT instruction; the BIOS occu-
  253. pies interrupts 10H through 1FH and also interrupt 5H; actually, of these
  254. seventeen interrupts, five are used for user exit points or data pointers,
  255. leaving twelve actual services.
  256. In most cases, a service deals with a particular hardware interface; for
  257. example, BIOS interrupt 10H deals with the screen.  As with DOS function
  258. calls, many BIOS services can be passed a function code in the AH register
  259. and possible other arguments.
  260. I am not going to summarize the most useful BIOS features here; you will
  261. see some examples in the next sample program we will look at.
  262. The other thing you might want to get into with the Tech reference is the
  263. description of some hardware options, particularly the asynch adapter,
  264. which are not well supported in the BIOS.  The writeup on the asynch adapt-
  265. er is pretty complete.
  266. Actually, the Tech reference itself is pretty complete and very nice as far
  267. as it goes.  One thing which is missing from the Tech reference is informa-
  268. tion on the programmable peripheral chips on the system board.  These
  269. include
  270.       the 8259 interrupt controller
  271.       the 8253 timer
  272.       the 8237 DMA controller and
  273.       the 8255 peripheral interface
  274. To make your library absolutely complete, you should order the INTEL data
  275. sheets for these beasts.
  276. I should say, though, that the only I ever found I needed to know about was
  277. the interrupt controller.  If you happen to have the 8086 Family User's
  278. Manual, the big book put out by INTEL, which is one of the things people
  279. sometimes buy to learn about 8086 architecture, there is an appendix there
  280. which gives an adequate description of the 8259.
  281.  
  282.  
  283. IBM PC Assembly Language Tutorial                                        24
  284.  
  285.  
  286. A final example
  287. _______________
  288. A final example
  289. A final example
  290. A final example
  291. I leave you with a more substantial example of code which illustrates some
  292. good elementary techniques; I won't claim its style is perfect, but I think
  293. it is adequate.  I think this is a much more useful example than what you
  294. will get with the assembler:
  295.           PAGE 61,132
  296.           TITLE SETSCRN -- Establish correct monitor use at boot time
  297. ;
  298. ;         This program is a variation on many which toggle the equipment flags
  299. ;         to support the use of either video option (monochrome or color).
  300. ;         The thing about this one is it prompts the user in such a way that he
  301. ;         can select the use of the monitor he is currently looking at (or which
  302. ;         is currently connected or turned on) without really having to know
  303. ;         which is which.  SETSCRN is a good program to put first in an
  304. ;         AUTOEXEC.BAT file.
  305. ;
  306. ;         This program is highly dependent on the hardware and BIOS of the IBMPC
  307. ;         and is hardly portable, except to very exact clones.  For this reason,
  308. ;         BIOS calls are used in lieu of DOS function calls where both provide
  309. ;         equal function.
  310. ;
  311. OK.  That's the first page of the program.  Notice the PAGE statement,
  312. which you can use to tell the assembler how to format the listing.  You
  313. give it lines per page and characters per line.  I have mine setup to print
  314. on the host lineprinter; I routinely upload my listings at 9600 baud and
  315. print them on the host; it is faster than using the PC printer.
  316. There is also a TITLE statement.  This simply provides a nice title for
  317. each page of your listing.  Now for the second page:
  318.           SUBTTL -- Provide .COM type environment and Data
  319.           PAGE
  320. ;
  321. ;         First, describe the one BIOS byte we are interested in
  322. ;
  323. BIOSDATA  SEGMENT   AT 40H    ;Describe where BIOS keeps his data
  324.           ORG       10H       ;Skip parts we are not interested in
  325. EQUIP     DB        ?         ;Equipment flag location
  326. MONO      EQU       00110000B ;These bits on if monochrome
  327. COLOR     EQU       11101111B ;Mask to make BIOS think of the color board
  328. BIOSDATA  ENDS                ;End of interesting part
  329. ;
  330. ;         Next, describe some values for interrupts and functions
  331. ;
  332. DOS       EQU       21H       ;DOS Function Handler INT code
  333. PRTMSG    EQU       09H       ;Function code to print a message
  334. KBD       EQU       16H       ;BIOS keyboard services INT code
  335. GETKEY    EQU       00H       ;Function code to read a character
  336. SCREEN    EQU       10H       ;BIOS Screen services INT code
  337. MONOINIT  EQU       02H       ;Value to initialize monochrome screen
  338. IBM PC Assembly Language Tutorial                                        25
  339.  
  340.